package com.xy.modulemain

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ToastUtils
import com.xy.modulemain.adapter.FragmentViewPageAdapter
import com.xy.modulemain.bean.PagingResult
import com.xy.modulemain.lisener.IBaseModelListener
import com.xy.modulemain.model.MainModel
import kotlinx.android.synthetic.main.mvvm_activity.*

/**
 * @ClassName MvvmActivity
 * @Author xy
 * @Date 2021/8/19 17:16
 * @Description TODO
 */
class MvvmActivity:AppCompatActivity(),IBaseModelListener<MutableList<String>> {
    var fragmentList = arrayListOf<Fragment>()
    var titleList = arrayListOf<String>()
    var adapter : FragmentViewPageAdapter?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.mvvm_activity)
//        var mainModel = MainModel(this)
//        mainModel.load() //请求网络
//        for (i in 0 until 12){
//            val tab = tabLayout.newTab()
//            tab.text = "tab${i+1}"
//            fragmentList.add(ItemFragment())
//            titleList.add("tab${i+1}")
//            tabLayout.addTab(tab)
//        }

//        var adapter = FragmentViewPageAdapter(supportFragmentManager,fragmentList,titleList)
        adapter = FragmentViewPageAdapter(supportFragmentManager)
//        viewPager.adapter = adapter
//        tabLayout.setupWithViewPager(viewPager)
//        viewPager.setCurrentItem(1, true) //设置默认的界面
    }

    override fun onLoadFinish(t: MutableList<String>, vararg pagingResult: PagingResult) {
        //设置tabLayout
       t.forEach {
           var tab = tabLayout.newTab()
           tab.text = it
           tabLayout.addTab(tab)
       }
        adapter?.setData(t)
        viewPager.adapter = adapter
        tabLayout.setupWithViewPager(viewPager)
    }

    override fun onLoadFailure(promat: String) {

        ToastUtils.showShort(promat)
    }


}